//////////////////////////////////////////////
// File				:	WinMain.cpp
//
// Author			:	Nick Faletra (NF)
//
// Date Created		:	12/20/2013
//////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include "Game/Game.h"
#include <ctime>

const char* g_szWindowClassName = "ArtemisWindowClass";
const char* g_szWindowTitle = "Project Artemis";
const int g_nWindowWidth = 800;
const int g_nWindowHeight = 600;

#ifdef _DEBUG
	const bool g_bWindowed = true;
#else
	const bool g_bWindowed = false;
#endif

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_SYSKEYUP:
		case WM_SYSCHAR:
		{
			return 0;
		}
		break;

		case WM_CLOSE:
		{
			DestroyWindow(hWnd);
		}
		break;

		// Lose/Gain focus
		case WM_ACTIVATE:
		{
			if (LOWORD(wParam) != WA_INACTIVE)
			{
				// Unpause game code here
			}
			else
			{
				// Pause game code here
			}
		}
		break;

		case WM_CREATE:
		{
			return 0;
		}
		break;

		case WM_PAINT:
		{
			return 0;
		}
		break;

		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

bool RegisterWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wnd;

	wnd.cbSize			= sizeof(WNDCLASSEX);
	wnd.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	wnd.lpfnWndProc		= WindowProc;
	wnd.cbClsExtra		= 0;
	wnd.cbWndExtra		= 0;
	wnd.hInstance		= hInstance;
	wnd.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wnd.hIconSm			= NULL;
	wnd.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wnd.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wnd.lpszMenuName	= NULL;
	wnd.lpszClassName	= g_szWindowClassName;

	return RegisterClassEx(&wnd) != 0;
}

HWND MakeWindow(HINSTANCE hInstance)
{
	DWORD dwWindowStyleFlags = WS_VISIBLE;
	dwWindowStyleFlags |= g_bWindowed ? WS_OVERLAPPEDWINDOW : WS_POPUP;
	ShowCursor(g_bWindowed);

	RECT rWindow;
	rWindow.left = 0;
	rWindow.top = 0;
	rWindow.right = g_nWindowWidth;
	rWindow.bottom = g_nWindowHeight;

	AdjustWindowRectEx(&rWindow, dwWindowStyleFlags, false, WS_EX_APPWINDOW);

	int nWindowWidth = rWindow.right - rWindow.left;
	int nWindowHeight = rWindow.bottom - rWindow.top;

	return CreateWindowEx(WS_EX_APPWINDOW, g_szWindowClassName, g_szWindowTitle, dwWindowStyleFlags,
						  (GetSystemMetrics(SM_CXSCREEN) >> 1) - (nWindowWidth >> 1),
						  (GetSystemMetrics(SM_CYSCREEN) >> 1) - (nWindowHeight >> 1),
						  nWindowWidth, nWindowHeight, NULL, NULL, hInstance, NULL);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	HWND hWnd;

	srand((unsigned int)time(0));

	if (!RegisterWindowClass(hInstance))
		return 0;

	hWnd = MakeWindow(hInstance);

	if (!hWnd)
		return 0;

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	CGame* pGame = new CGame();
	pGame->Initialize(hWnd, hInstance, g_nWindowWidth, g_nWindowHeight, g_bWindowed);

	while (true)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if (!pGame->Main())
			break;
	}

	pGame->Shutdown();
	delete pGame;
	pGame = nullptr;

	UnregisterClass(g_szWindowClassName, hInstance);

	return (int)(msg.wParam);
}